<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { Descriptions } from '@/components/Descriptions'
import { DescriptionsSchema } from '@/types/descriptions'
import { PropType, reactive, watch } from 'vue'
import { FormSchema } from '@/types/form'
import { OrgData } from "@/api/org/types"

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<OrgData>>,
    default: () => null
  },
  currentOrg: {
    type: Object as PropType<Nullable<OrgData>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  }
})

const rules = reactive({})

const { register, methods, elFormRef } = useForm({
  schema: props.formSchema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

const detailSchema = reactive<DescriptionsSchema[]>([
  {
    field: 'type',
    label: '用户类型',
    labelClassName: 'detailLabelClass'
  },
  {
    field: 'idType',
    label: 'ID分配规则',
    labelClassName: 'detailLabelClass'
  },
  {
    field: 'companyId',
    label: '组织ID'
  },
  {
    field: 'name',
    label: '组织名称'
  },
  {
    field: 'code',
    label: '组织机构代码/身份证号码'
  },
  {
    field: 'apply',
    label: '申请人姓名'
  },
  {
    field: 'phone',
    label: '申请人电话'
  },
  {
    field: 'email',
    label: '申请人邮箱'
  },
  {
    field: 'supervisor',
    label: '监督人'
  },
  {
    field: 'supervisorPhone',
    label: '监督人电话'
  },
  {
    field: 'license',
    label: '营业执照'
  }
])

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})

</script>

<template>
  <Descriptions :data="props.currentOrg" :schema="detailSchema">
    <template #license="{ row }">
      <ElImage :src="row.license" />
    </template>
  </Descriptions>
  <Form :rules="rules" @register="register" />
</template>

<style lang="less">
.detailLabelClass {
  width: 200px !important;
}
</style>
